package org.liuping.interceptor;

import cn.dev33.satoken.exception.NotLoginException;
import org.liuping.exception.CodeErrorException;
import org.liuping.exception.PasswordConfirmException;
import org.liuping.exception.UsernameOrEmailExistException;
import org.liuping.exception.UsernamePasswordErrorException;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.ModelAndView;


import java.util.HashMap;
import java.util.Map;

/**
 * 全局异常配置（跳转页面，没有加rest）
 */
@ControllerAdvice
public class MyGlobalException {
    /**
     * 处理参数校验异常(MethodArgumentNotValidException)
     */
    @ExceptionHandler(value = MethodArgumentNotValidException.class)
    public ModelAndView validHandler(MethodArgumentNotValidException e){

        ModelAndView modelAndView = new ModelAndView();
        //获取参数校验的所有异常信息
        BindingResult bindingResult = e.getBindingResult();
        Map<String,String> errorMap = new HashMap<>();
        for (FieldError fieldError : bindingResult.getFieldErrors()) {
            //获取出错字段以及错误信息
            errorMap.put(fieldError.getField(),fieldError.getDefaultMessage());
        }
        String s = errorMap.toString();
        modelAndView.addObject("message",s);
        modelAndView.setViewName("error");
        return modelAndView;
    }
    //用户名或密码错误异常
    @ExceptionHandler(UsernamePasswordErrorException.class)
    public ModelAndView handlerUsernamePasswordException(UsernamePasswordErrorException e){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("message",e.getMessage());
        modelAndView.setViewName("error");
        return modelAndView;
    }
    //sa-token的未登录异常
    @ExceptionHandler(NotLoginException.class)
    public ModelAndView handlerNotLoginException(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("message","请登录后再操作");
        modelAndView.setViewName("error");
        return modelAndView;
    }
    //注册时两次输入密码不匹配
    @ExceptionHandler(PasswordConfirmException.class)
    public ModelAndView handlerPasswordConfirmException(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("message","两次输入密码不匹配");
        modelAndView.setViewName("error");
        return modelAndView;
    }
    //用户名或邮箱已存在异常
    @ExceptionHandler(UsernameOrEmailExistException.class)
    public ModelAndView handlerUsernameOrEmailExistException(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("message","用户名或邮箱已存在");
        modelAndView.setViewName("error");
        return modelAndView;
    }
    //验证码不正确
    @ExceptionHandler(CodeErrorException.class)
    public ModelAndView handlerCodeErrorException(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("message","验证码错误");
        modelAndView.setViewName("error");
        return modelAndView;
    }
    // 其余全局异常拦截
    @ExceptionHandler
    public ModelAndView handlerException(Exception e) {
        e.printStackTrace();
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("message","网络异常，请稍后再试");
        modelAndView.setViewName("error");
        return modelAndView;
    }
}
